package com.unlcn.ils.wms.web.utils.JMSUtil;

import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.MessageListener;

/**
 * Created by DELL on 2017/8/28.
 */
public class JMSConsumerTest implements MessageListener {
    /*public static void main(String[] args) throws Exception {

        /*//**  JMSConsumer 可以设置成全局的静态变量，只需实例化一次即可使用,禁止循环重复实例化JMSConsumer(因为其内部存在一个线程池)

        JMSConsumer consumer = new JMSConsumer();
        consumer.setBrokerUrl("tcp://10.20.30.111:61616");
        consumer.setQueue("asn");
        consumer.setUserName("admin");
        consumer.setPassword("admin");
        consumer.setQueuePrefetch(500);
        consumer.setMessageListener(new MultiThreadMessageListener(50,new MessageHandler() {
            public void handle(Message message) {
                try {
                    System.out.println("name is " + ((MapMessage)message).getString("name"));
                    Thread.sleep(5000);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }));
        consumer.start();

//      Thread.sleep(5000);
//      consumer.shutdown();

    }*/

    @Override
    public void onMessage(Message message){
        JMSConsumer consumer = new JMSConsumer();
        consumer.setBrokerUrl("tcp://10.20.30.111:61616");
        consumer.setQueue("asn");
        consumer.setUserName("admin");
        consumer.setPassword("admin");
        consumer.setQueuePrefetch(500);
        consumer.setMessageListener(new MultiThreadMessageListener(50,new MessageHandler() {
            public void handle(Message message) {
                try {
                    System.out.println("name is " + ((MapMessage)message).getString("name"));
                    Thread.sleep(5000);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }));
        try {
            consumer.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
